<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Project_group extends CI_Controller {
	public $user_m;
	public function __construct(){
		parent::__construct();
		$this->load->model('project_group_m');
	}
	public function index(){
		$where=array();
		$where['project_group.is_del']='1';
		$num=$this->project_group_m->getProjectGListNum($where);
		if($_POST){
			$post=$this->input->post();
			$page=$post['post_page'];
		}
		else{
			$page=$this->uri->segment(3);
		}
		$this->load->library('pagination');
		$config['base_url'] = site_url().'/project_group /index';
		$config['total_rows'] = $num;
		$config['per_page'] = 10; 
		$config['uri_segment'] = 3;
		//$config['page_query_string'] = true;
		$config['is_ajax'] = true;
		$config['script_fun'] = 'pagejump';
		$this->pagination->initialize($config); 
		$pager=$this->pagination->create_links();
		$list=$this->project_group_m->getProjectGList($where,$config['per_page'],$page);
		if($_POST){
			$html='';
			$html_data[0]['list']=$list;
			$html_data[0]['pager']=$pager;
			echo json_encode($html_data);
			exit;
		}
		
		$data['list']=$list;
		$data['pager']=$pager;
		$data['css']=$this->config->item('admin_css');		
		$data['js']=$this->config->item('admin_js');
		$js_view=$this->config->item('view_js_url');
		array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css');
		array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js',$js_view.'project_group/project_group_list.js');
		$this->load->view('admin/header',$data);
		$this->load->view('project_group/project_group_list');
		$this->load->view('admin/footer');
	}
	/**
	 * 添加项目组
	 *
	 */
	public function add_project_group(){
		$post=$this->input->post();
		if($post){
			//项目名称是否重复
			$nums=$this->project_group_m->isSameName($post['name']);
			if($nums>0){
				$return_array[0]['status']='0';
				$return_array[0]['message']='添加失败,用户名重复';
				echo json_encode($return_array);
				exit;
			}
			$return_array=array();
			$a=array();
			$a['name']=$post['name'];
			$a['status']=$post['status'];
			$a['leader_uid']=$post['leader_uid'];
			$return=$this->project_group_m->addProjectGroup($a,1);			
			if($return){	
				$return_array[0]['status']='1';
				$return_array[0]['message']='添加成功';
				$return_array[0]['insert_id']=$return;
				echo json_encode($return_array);
				exit;
			}
			else{
				$return_array[0]['status']='0';
				$return_array[0]['message']='添加失败,请检查数据';
				echo json_encode($return_array);
				exit;
			}
		}
		else{
			$data['css']=$this->config->item('admin_css');		
			$data['js']=$this->config->item('admin_js');
			$js_view=$this->config->item('view_js_url');
			array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css','/admin_style/css/jquery.autocomplete.css');
			array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js','/admin_style/js/jquery.validate.js','/admin_style/js/jquery.metadata.js','/admin_style/js/jquery.autocomplete.js',$js_view.'project_group/add_project_group.js');
			$this->load->view('admin/header',$data);
			$this->load->view('project_group/add_project_group');
			$this->load->view('admin/footer');
		}
	}
	/**
	 * 编辑项目组
	 *
	 */
	public function update_project_group($id=''){
		$post=$this->input->post();
		if($post){
			$return_array=array();
			$a=$where=array();
			$a['name']=$post['name'];
			$a['leader_uid']=$post['leader_uid'];
			$a['status']=$post['status'];
     		$where['id']=$post['pid'];
			$return=$this->project_group_m->updateProjectGroup($a,$where);			
			if($return){	
				$return_array[0]['status']='1';
				$return_array[0]['message']='修改成功';
				$return_array[0]['insert_id']=$return;
				echo json_encode($return_array);
				exit;
			}
			else{
				$return_array[0]['status']='0';
				$return_array[0]['message']='修改失败,请检查数据';
				echo json_encode($return_array);
				exit;
			}
		}
		else{
			if(empty($id)){
				showmessage('数据错误','/index.php/project_group');
			}
			
			$project_group_list=$this->project_group_m->getProjectInfoById($id);
			if(!empty($project_group_list)){
				$data['pid']=$id;
				$this->load->model('user_m');
				$chname=$this->user_m->getUserChnameById($project_group_list['leader_uid']);
				$project_group_list['leader_name']=$chname;
				$data['project_group_list']=$project_group_list;
			}
			else{
				showmessage('数据错误','/index.php/project_group');
			}
			$data['css']=$this->config->item('admin_css');		
			$data['js']=$this->config->item('admin_js');
			$js_view=$this->config->item('view_js_url');
			array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css','/admin_style/css/jquery.autocomplete.css');
			array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js','/admin_style/js/jquery.validate.js','/admin_style/js/jquery.metadata.js','/admin_style/js/jquery.autocomplete.js',$js_view.'project_group/update_project_group.js');
			$this->load->view('admin/header',$data);
			$this->load->view('project_group/update_project_group');
			$this->load->view('admin/footer');
		}
	}
	/**
	 * 添加项目组成员
	 *
	 */
	public function add_user_project_group($id=''){
		$post=$this->input->post();
		if($post){
                $a=$return_array=array();
                if(empty($post['id'])  || empty($post['uid_list'])){
				$return_array[0]['status']='0';
				$return_array[0]['message']='项目组人员不能为空';
				echo json_encode($return_array);
				exit;
			}
             if (!empty($post['uid_list'])) {
                        $uid_list=substr($post['uid_list'],0,-1);
                        $user_list=explode(",", $uid_list);
             }
             $a['user_list']=$user_list;
             $a['id']=$post['id'];
             $query=$this->project_group_m->addUserProjectGroup($a);
             if($query){
                    $return_array[0]['status']='1';
                    $return_array[0]['message']='添加成功';
                    echo json_encode($return_array);
                    exit;
            }
            else{
                    $return_array[0]['status']='0';
                    $return_array[0]['message']='添加失败,请检查数据';
                    echo json_encode($return_array);
                    exit;
            }
		}
		else {
			if(empty($id)){
				showmessage('数据错误','/index.php/project_group');
			}
            $project_group_list=$this->project_group_m->getProjectInfoById($id);
            $user_list=$this->project_group_m->getUserListFUserproject($id);
            if(!empty($user_list['0'])){
                $data['user_list']=$user_list;
            }
            	$data['project_group_list']=$project_group_list;
				$data['css']=$this->config->item('admin_css');		
				$data['js']=$this->config->item('admin_js');
				$js_view=$this->config->item('view_js_url');
				array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css','/admin_style/css/jquery.autocomplete.css');
				array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js','/admin_style/js/jquery.validate.js','/admin_style/js/jquery.metadata.js','/admin_style/js/jquery.autocomplete.js',$js_view.'project_group/add_user_project_group.js');
				$this->load->view('admin/header',$data);
				$this->load->view('project_group/add_user_project_group');
				$this->load->view('admin/footer');
			}
	}
	public function del_project_group(){
		if($_POST){
			$post=$this->input->post();
			if(empty($post['id']))
			{
				return false;
			}
			$return_array=array();
			$id=$post['id'];
			$query=$this->project_group_m->delProjectGroup($id);
			if($query){
				$return_array[0]['status']='1';
				$return_array[0]['message']='删除成功';
				echo json_encode($return_array);
				exit;
			}
			else{
				$return_array[0]['status']='0';
				$return_array[0]['message']='删除失败,请检查数据';
				echo json_encode($return_array);
				exit;
			}
		}
		else{
			$return_array[0]['status']='0';
			$return_array[0]['message']='数据错误';
			echo json_encode($return_array);
			exit;
		}
	}
        public function del_user_project_group($id=''){
            $post=$this->input->post();
			if($post){
						if(!empty($post['new_uid_list'])){
							$a_new_uid_list=explode(',',$post['new_uid_list']);
							$a_uid_list=explode(',',$post['uid_list']);
							$diff_arr=array_diff($a_uid_list,$a_new_uid_list);
							$query=$this->project_group_m->delUserProjectGroup($post['pg_id'],$diff_arr);
							$return_array=array();
							if($query){
								$return_array[0]['status']='1';
	                            $return_array[0]['message']='删除成功';
	                            echo json_encode($return_array);
	                            exit;
							}
							else{
								$return_array[0]['status']='0';
	                            $return_array[0]['message']='删除失败,请检查数据';
	                            echo json_encode($return_array);
	                            exit;
							}
						}
						else{
							//当new_uid_list为空的时候 就是全部删除了
							$a_uid_list=explode(',',$post['uid_list']);
							$query=$this->project_group_m->delUserProjectGroup($post['pg_id'],$a_uid_list);
							if($query){
								$return_array[0]['status']='1';
	                            $return_array[0]['message']='删除成功';
	                            echo json_encode($return_array);
	                            exit;
							}
							else{
								$return_array[0]['status']='0';
	                            $return_array[0]['message']='删除失败,请检查数据';
	                            echo json_encode($return_array);
	                            exit;
							}
						}
	                }
	                
	                else{
	                   		if(empty($id)){
								showmessage('数据错误','/index.php/project_group');
							}
	                        $project_group_list=$this->project_group_m->getProjectInfoById($id);
	                        $user_list=$this->project_group_m->getUserListFUserproject($id);
	                        if(!empty($user_list['0'])){
	                            $data['user_list']=$user_list;
	                            foreach ($user_list as $k=>$v){
	                            	$uid_list[]=$v['uid'];
	                            }
	                            $uid_list=implode(',',$uid_list);
	                            $data['uid_list']=$uid_list;
	                        } 
	                        $data['project_group_list']=$project_group_list;
							$data['css']=$this->config->item('admin_css');
							$data['js']=$this->config->item('admin_js');
							$js_view=$this->config->item('view_js_url');
							array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css','/admin_style/css/jquery.autocomplete.css');
							array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js','/admin_style/js/jquery.validate.js','/admin_style/js/jquery.metadata.js','/admin_style/js/jquery.autocomplete.js',$js_view.'project_group/del_user_project_group.js');
							$this->load->view('admin/header',$data);
							$this->load->view('project_group/del_user_project_group');
							$this->load->view('admin/footer');
	                }
        }
	/**
	 * 分配项目组项目
	 *
	 */
	public function add_p_project_group($id=''){
		$post=$this->input->post();
		if($post){
                $a=$return_array=array();
                if(empty($post['p_project_list'])){
					$return_array[0]['status']='0';
					$return_array[0]['message']='请选择项目';
					echo json_encode($return_array);
					exit;
				}
             if (!empty($post['p_project_list'])) {
                        $p_project_list=substr($post['p_project_list'],0,-1);
             }
             $a['project_list']=$p_project_list;
             $where['id']=$post['pg_id'];
             $query=$this->project_group_m->addProjectToProjectGroup($a,$where);
             if($query){
                    $return_array[0]['status']='1';
                    $return_array[0]['message']='添加成功';
                    echo json_encode($return_array);
                    exit;
            }
            else{
                    $return_array[0]['status']='0';
                    $return_array[0]['message']='添加失败,请检查数据';
                    echo json_encode($return_array);
                    exit;
            }
		}
		else {
			if(empty($id)){
				showmessage('数据错误','/index.php/project_group');
			}
            $project_group_list=$this->project_group_m->getProjectInfoById($id);
            $where['status']='1';
            $this->load->model('project_m');
            $project_list=$this->project_m->getProjetList($where);
            $data['project_list']=$project_list;

            if(!empty($user_list['0'])){
                $data['user_list']=$user_list;
            }
            	$data['project_group_list']=$project_group_list;
            	$data['project_list_arr']=explode(',',$project_group_list['project_list']);

				$data['css']=$this->config->item('admin_css');		
				$data['js']=$this->config->item('admin_js');
				$js_view=$this->config->item('view_js_url');
				array_push($data['css'],'/admin_style/css/Skins/Blue/jbox.css','/admin_style/css/jquery.autocomplete.css');
				array_push($data['js'],'/admin_style/js/jquery.jBox-2.3.min.js','/admin_style/js/i18n/jquery.jBox-zh-CN.js','/admin_style/js/jquery.validate.js','/admin_style/js/jquery.metadata.js','/admin_style/js/jquery.autocomplete.js',$js_view.'project_group/add_p_project_group.js');
				$this->load->view('admin/header',$data);
				$this->load->view('project_group/add_p_project_group');
				$this->load->view('admin/footer');
			}
	}
}